﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for XThicknessEditor.xaml
    /// </summary>
    public partial class XThicknessEditor : XPropertyValueEditor
    {
        public static DependencyProperty PropertyValueProperty = DependencyProperty.Register("PropertyValue", typeof(Thickness), typeof(XThicknessEditor), new PropertyMetadata(propertyChangedCallback));
        private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            XThicknessEditor editor = d as XThicknessEditor;
            if (e.NewValue != null)
            {
                Thickness thickness = (Thickness)e.NewValue;
                editor._ctlLeft.Text = thickness.Left.ToString();
                editor._ctlTop.Text = thickness.Top.ToString();
                editor._ctlRight.Text = thickness.Right.ToString();
                editor._ctlBottom.Text = thickness.Bottom.ToString();
            }
            else
            {
                editor._ctlLeft.Text = "";
                editor._ctlTop.Text = "";
                editor._ctlRight.Text = "";
                editor._ctlBottom.Text = "";
            }
        }
        protected override void OnXInit(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
        {
            Binding binding = new Binding();
            binding.Source = propertyItem;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            this.SetBinding(PropertyValueProperty, binding);
        }
        protected override DependencyProperty GetPropertyValue()
        {
            return PropertyValueProperty;
        }
        public XThicknessEditor()
        {
            InitializeComponent();
            _ctlLeft.LostFocus += _ctlLeft_LostFocus;
            _ctlTop.LostFocus += _ctlLeft_LostFocus;
            _ctlRight.LostFocus += _ctlLeft_LostFocus;
            _ctlBottom.LostFocus += _ctlLeft_LostFocus;
        }

        protected override void OnInitXPropertyValue(XPropertyAttribute xProperty, System.Reflection.PropertyInfo propertyInfo, object value)
        {
        }
        protected override void OnXInit(Part partInstance, XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
            Binding binding = new Binding();
            binding.Source = partInstance;
            binding.Path = new PropertyPath(propertyInfo.Name);
            this.SetBinding(PropertyValueProperty, binding);
        }

        private void _ctlLeft_LostFocus(object sender, RoutedEventArgs e)
        {
            ValueChanged();
        }

        private void _ctlTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ValueChanged();
            }
        }

        private void ValueChanged()
        {
            Thickness ret = new Thickness();
            double d;
            if (double.TryParse(_ctlLeft.Text, out d))
            {
                ret.Left = d;
            }
            if (double.TryParse(_ctlTop.Text, out d))
            {
                ret.Top = d;
            }
            if (double.TryParse(_ctlRight.Text, out d))
            {
                ret.Right = d;
            }
            if (double.TryParse(_ctlBottom.Text, out d))
            {
                ret.Bottom = d;
            }
            UpdateXPropertyValue(ret);
        }

        protected override void OnUnDo(object oldValue)
        {
            base.OnUnDo(oldValue);

            Thickness thickness = (Thickness)oldValue;
            _ctlLeft.Text = thickness.Left.ToString();
            _ctlTop.Text = thickness.Top.ToString();
            _ctlRight.Text = thickness.Right.ToString();
            _ctlBottom.Text = thickness.Bottom.ToString();
        }
    }
}
